package com.pengpeng.redis.controller;

import com.pengpeng.redis.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author pengp
 * @date 2022年03月31日 16:50
 */
@RestController
public class TestController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/add-topic")
    public String addTopic(String message) {
        messageService.sendTopicMessage(message);
        return "success";
    }

    @PostMapping("/add-queue")
    public String addQueue(String message) {
        messageService.sendQueueMessage(message);
        return "success";
    }

    @RequestMapping(value = "/redisTest", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public Map<String,Object> redisTest(){
        //redis中存入5秒失效的key
        redisTemplate.opsForValue().set("myKey", "myValue",5, TimeUnit.SECONDS);
        String myKey = redisTemplate.opsForValue().get("myKey");
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("myKey",myKey);
        return resultMap;
    }
}
